{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "importing-necessary-libraries",
   "metadata": {},
   "source": [
    "## Importing Necessary Libraries\n",
    "\n",
    "We start by importing the necessary libraries for data manipulation, downloading financial data, and plotting."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "code-imports",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.dates as mdates\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "explanation-imports",
   "metadata": {},
   "source": [
    "Here is what each library does:\n",
    "- **pandas**: Used for data manipulation and analysis.\n",
    "- **matplotlib.pyplot**: A plotting library for creating static, animated, and interactive visualizations.\n",
    "- **matplotlib.dates**: Helps in formatting dates in matplotlib plots."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "creating-dataframe",
   "metadata": {},
   "source": [
    "## Creating a Pandas DataFrame with S&P 500 Futures Data\n",
    "\n",
    "We read the S&P 500 futures data from a CSV file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "code-read-csv",
   "metadata": {},
   "outputs": [],
   "source": [
    "sp500_data = pd.read_csv(\"../data/sp500.csv\", index_col=0, parse_dates=True)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "explanation-read-csv",
   "metadata": {},
   "source": [
    "We read the data from `data/sp500.csv` using `pd.read_csv()`. The `index_col=0` parameter sets the first column as the index, and `parse_dates=True` ensures that the index is in datetime format, which is important for time series analysis."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "computing-daily-returns",
   "metadata": {},
   "source": [
    "## Computing Daily Returns\n",
    "\n",
    "We calculate the daily percentage change in the adjusted closing prices."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "code-compute-returns",
   "metadata": {},
   "outputs": [],
   "source": [
    "returns = None  # YOUR CODE HERE"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "calculating-cumulative-returns",
   "metadata": {},
   "source": [
    "## Calculating Cumulative Returns\n",
    "\n",
    "We compute the cumulative returns over the period."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "code-cumulative-returns",
   "metadata": {},
   "outputs": [],
   "source": [
    "cumulative_return = None  # YOUR CODE HERE"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "explanation-cumulative-returns",
   "metadata": {},
   "source": [
    "By adding 1 to the daily returns and taking the cumulative product, we obtain the compounded returns over time."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "plotting-cumulative-returns",
   "metadata": {},
   "source": [
    "## Plotting Cumulative Returns\n",
    "\n",
    "Finally, we plot the cumulative returns over time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code-plotting",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(10, 6))\n",
    "plt.plot(cumulative_return - 1, label='Cumulative Returns')\n",
    "plt.title('S&P500 Front-Month Futures Cumulative Returns')\n",
    "plt.xlabel('Year')\n",
    "plt.ylabel('Cumulative Returns')\n",
    "# Setting x-axis major locator to each year and formatter\n",
    "plt.gca().xaxis.set_major_locator(mdates.YearLocator())\n",
    "plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y'))\n",
    "# Adding grid with vertical lines for each year\n",
    "plt.grid(True, which='major', linestyle='--', color='grey')\n",
    "# Rotate x-axis labels by 45 degrees\n",
    "plt.xticks(rotation=45)\n",
    "plt.legend()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "explanation-plotting",
   "metadata": {},
   "source": [
    "We create a plot to visualize the cumulative returns:\n",
    "- **Figure Size**: Sets the size of the plot for better visibility.\n",
    "- **Plotting**: Plots the cumulative returns over time.\n",
    "- **Title and Labels**: Adds a title and axis labels.\n",
    "- **X-Axis Formatting**: Formats the x-axis to show years using `YearLocator` and `DateFormatter`.\n",
    "- **Grid**: Adds a grid with vertical lines at each year for better readability.\n",
    "- **Rotation**: Rotates the x-axis labels to prevent overlap.\n",
    "- **Legend**: Displays the legend.\n",
    "- **Display**: Shows the final plot."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
